package com.taptech.doufu.services.personalcenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.taobao.weex.bridge.JSCallback;
import com.taptech.doufu.event.EventBusInterestTag;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.umeng.push.PushManager;
import com.taptech.doufu.umeng.UVerifyManager;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.util.HttpRequestUtil;
import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.bean.cp.CpBean;
import com.taptech.doufu.services.BaseService;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.MyAccountOperation;
import com.taptech.doufu.bean.MyAccount;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.EmailAccount;
import com.taptech.doufu.bean.personalcenter.PersonalBaseAccount;
import com.taptech.doufu.bean.personalcenter.PhoneAccount;
import com.taptech.doufu.bean.personalcenter.QQAccount;
import com.taptech.doufu.bean.personalcenter.WeiboAccount;
import com.taptech.doufu.bean.personalcenter.WeixinAccount;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.net.httputils.HttpUtil;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;
import com.taptech.doufu.weex.http.TFHttpError;
import com.taptech.doufu.weex.http.TFHttpResponse;
import com.taptech.doufu.weex.http.TFHttpUtil;
import com.taptech.doufu.weex.notification.QLXNotificationCenter;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class AccountService extends BaseService implements HttpResponseListener {
    private PersonalBaseAccount baseAccount;
    private QQAccount qqAccount;
    private WeiboAccount weiboAccount;
    private PhoneAccount phoneAccount;
    private EmailAccount emailAccount;

    public WeixinAccount getWeixinAccount() {
        return weixinAccount;
    }

    public void setWeixinAccount(WeixinAccount weixinAccount) {
        this.weixinAccount = weixinAccount;
    }

    private WeixinAccount weixinAccount;

    private int loginType;
    private boolean isAccountBack;
    private boolean socialLogin;

    public static ArrayList<CpBean> userCps = new ArrayList<CpBean>();

    private static AccountService instance = new AccountService();
    public static final String USER_INFO_PATH = Constant.AppDir.DIR_CACHE + "data/account/";

    public static Context context = WeMediaApplication.applicationContext;


    private static final String DEFAUL_URL = "http://" + Constant.HOST_NAME + "/index.php/member/";

    private static final String PHONE_ACCOUNT = "phone";
    private static final String EMAIL_ACCOUNT = "email";
    private static final String WEIBO_ACCOUNT = "weibo";
    private static final String QQ_ACCOUNT = "qq";
    private static final String WEIXIN_ACCOUNT = "weixin";
    private static final String IS_LOGIN = "isLogin";
    public static final String ACCOUNT = "account";

    public static final int ACCOUNT_TYPE_PHONE = 0;
    public static final int ACCOUNT_TYPE_WEIBO = 1;
    public static final int ACCOUNT_TYPE_QQ = 2;
    public static final int ACCOUNT_TYPE_WECHAT = 4;
    public static final int ACCOUNT_TYPE_EMAIL = 3;

    public static final int HANDLE_LOAD_ACCOUNT = 1003;
    public static final int HANDLE_MODIFY_SEX2 = 2006;
    public static final int HANDLE_LOAD_USER_WEIBO_INFO = 1006;

    private Bitmap userPortarit;
    private String userPortaritUrl;
    private boolean infoChanged, cacheLogin;
    public Set<String> userReadOrders = new HashSet<>();

    private AccountService() {

    }

    public static boolean hasBindMobile() {
        try {
            String jsonValue = DiaoBaoSharedPreferences.getSharedPreferencesValueToString("BindMobiled"+AccountService.getInstance().getUserUid(), context, "");
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonValue);
            int hasBind = jsonObject.getIntValue("hasBind");
            return hasBind > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static AccountService getInstance() {
        return instance;
    }


    // 第一次注册时初始化缓存的帐号数据
    public void initLocalCachAccount(final PersonalBaseAccount account) {
        try {
            String userId = account.getUid();
            if (userId != null && !userId.equals("")) {
                JSONObject saveData = null;
                saveData = account.getJson();
                saveData.put("isLogin", true);
                saveData.put(Constant.LOGIN_TYPE, account.getJson().getInt(Constant.LOGIN_TYPE));
                saveCacheAccount(saveData);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    public static JSONObject getCacheAccount(String userId) {
        JSONObject saveData = null;
        try {
            if (userId != null && !userId.equals("")) {
                String jsonString = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE).getString(ACCOUNT, "");
                if (!TextUtils.isEmpty(jsonString))
                    saveData = new JSONObject(jsonString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return saveData;
    }

    public JSONObject saveCacheAccount(JSONObject saveData) {
        try {
            String userId = saveData.getString(Constant.UID);
            if (userId != null && !userId.equals("")) {
                context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE).edit().putString(ACCOUNT, saveData.toString(0)).commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return saveData;
    }

    public void bindAccount(final JSONObject personInfo, final int accountType) {
        try {
            String userId = DiaobaoUtil.getStringFromJSONObject(personInfo, Constant.UID);
            if (userId == null || userId.equals("")) {
                userId = AccountService.getInstance().getBaseAccount().getUid();
                personInfo.put(Constant.UID, userId);
            }
            if (userId != null && !userId.equals("")) {

                JSONObject saveData = getCacheAccount(userId);
                String typeKey = PHONE_ACCOUNT;
                if (accountType == ACCOUNT_TYPE_PHONE) {
                    PhoneAccount phone = new PhoneAccount();
                    phone.setJson(personInfo);
                    setPhoneAccount(phone);
                } else if (accountType == ACCOUNT_TYPE_WEIBO) {
                    typeKey = WEIBO_ACCOUNT;
                    WeiboAccount weibo = new WeiboAccount();
                    weibo.setJson(personInfo);
                    setWeiboAccount(weibo);
                } else if (accountType == ACCOUNT_TYPE_QQ) {
                    typeKey = QQ_ACCOUNT;
                    QQAccount qq = new QQAccount();
                    qq.setJson(personInfo);
                    setQqAccount(qq);
                } else if (accountType == ACCOUNT_TYPE_EMAIL) {
                    typeKey = EMAIL_ACCOUNT;
                    EmailAccount email = new EmailAccount();
                    email.setJson(personInfo);
                    setEmailAccount(email);
                } else if (accountType == ACCOUNT_TYPE_WECHAT) {
                    typeKey = WEIXIN_ACCOUNT;
                    WeixinAccount weixinAccount = new WeixinAccount();
                    weixinAccount.setJson(personInfo);
                    setWeixinAccount(weixinAccount);
                }
                saveData.put(typeKey, personInfo);
                saveCacheAccount(saveData);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // 用户登录凳出时修改登录标识
    public void setLocalLoginFlag(String userId, boolean flag) {
        try {

            JSONObject accountInfo = getCacheAccount(userId);
            accountInfo.put(IS_LOGIN, flag);
            saveCacheAccount(accountInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void setLocalCounts(PersonalBaseAccount account) {
        try {

            JSONObject accountInfo = getCacheAccount(account.getUid());
            accountInfo.put("activity_num", account.getActivity_num());
            accountInfo.put("fans_counts", account.getFans_counts());
            accountInfo.put("attentions_counts", account.getAttentions_counts());
            saveCacheAccount(accountInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void setLocalCps(PersonalBaseAccount account) {
        try {
            userCps.clear();
            userCps.addAll(account.getCps());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void loadLocalCounts() {
        try {
            JSONObject accountInfo = getCacheAccount(baseAccount.getUid());
            if (accountInfo.has("activity_num"))
                baseAccount.setActivity_num(DiaobaoUtil.getStringFromJSONObject(accountInfo, "activity_num"));
            if (accountInfo.has("fans_counts"))
                baseAccount.setFans_counts(DiaobaoUtil.getStringFromJSONObject(accountInfo, "fans_counts"));
            if (accountInfo.has("attentions_counts"))
                baseAccount.setAttentions_counts(DiaobaoUtil.getStringFromJSONObject(accountInfo, "attentions_counts"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取个人信息 HandleType使用 AccountService.HANDLE_LOAD_ACCOUNT
     *
     * @param listener
     */
    @SuppressWarnings("rawtypes")
    public void loadAccountInfo(HttpResponseListener listener) {

        try {
            JSONObject accountInfo = null;
            // 转移老用户的登录态
            if (Constant.exitsSDCard) {
                File accounts = new File(USER_INFO_PATH);
                if (accounts != null && accounts.exists()) {
                    File userFiles[] = accounts.listFiles();
                    File userFile = DiaobaoUtil.getLastModifyFile(userFiles);
                    if (userFile != null) {
                        accountInfo = CacheUtil.readJsonDataFromCache(userFile);
                        if (accountInfo != null && accountInfo.has(IS_LOGIN) && accountInfo.getBoolean(IS_LOGIN)) {
                            File cookieFile = new File(Constant.AppDir.FILE_CACHE_DATA + "/cookie" + accountInfo.getString(Constant.UID));
                            if (cookieFile != null && cookieFile.exists()) {
                                setLocalLoginFlag("", true);
                                InputStream is = null;
                                ObjectInputStream ois = null;
                                is = new FileInputStream(cookieFile);
                                ois = new ObjectInputStream(is);
                                HashMap<String, Object> values = (HashMap<String, Object>) ois.readObject();
                                HttpRequestUtil.storeCookie(values, accountInfo.getString(Constant.UID));
                                saveCacheAccount(accountInfo);
                                cookieFile.delete();
                            }
                        }
                    }
                }
            }
            String account = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE).getString(ACCOUNT, null);
            TTLog.s("account=========" + account);
            if (account != null) {
                accountInfo = new JSONObject(account);
            }
            if (accountInfo != null && accountInfo.has(IS_LOGIN) && accountInfo.getBoolean(IS_LOGIN)) {

                String userId = accountInfo.getString(Constant.UID);
                /** 修复登录丢失 */
                baseAccount = new PersonalBaseAccount();

                baseAccount.setJson(accountInfo);

                setLoginType(accountInfo.getInt(Constant.LOGIN_TYPE));

                cacheLogin = true;
                if (DiaobaoUtil.isInteger(userId)) {
                    HttpRequestObject request = new HttpRequestObject();
                    request.setHandleType(HANDLE_LOAD_ACCOUNT);
                    request.setUrl(DEFAUL_URL + "load_user_info?uid=" + userId);
                    if (listener == null) {
                        request.setListener(this);
                    } else {
                        request.setListener(listener);
                    }
                    HttpUtil.sendGetRequest(request);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean existUserCache(String uid) {
        try {
            if (baseAccount == null)
                return false;
            String stringObj = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE).getString(ACCOUNT, null);
            if (stringObj == null)
                return false;
            JSONObject jsonObj = new JSONObject(stringObj);
            if (jsonObj.has(Constant.UID))
                return true;
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public boolean accountIsLogin() {
        try {
            JSONObject accountInfo = null;
            Map map = context.getSharedPreferences(ACCOUNT, Context.MODE_PRIVATE).getAll();
            Object keys[] = map.keySet().toArray();
            for (Object key : keys) {
                String account = map.get(key).toString();
                JSONObject obj = new JSONObject(account);
                if (obj.has(IS_LOGIN) && obj.getBoolean(IS_LOGIN)) {
                    accountInfo = obj;
                    break;
                }
            }
            if (accountInfo != null && accountInfo.has(IS_LOGIN) && accountInfo.getBoolean(IS_LOGIN)) {
                setLoginType(DiaobaoUtil.getIntFromJSONObject(accountInfo, Constant.LOGIN_TYPE));
                baseAccount = new PersonalBaseAccount();
                baseAccount.setJson(accountInfo);


                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    public void getSexuality(HttpResponseListener listener) {
        HttpRequestObject request = new HttpRequestObject();
        request.setHandleType(HANDLE_MODIFY_SEX2);
        request.setUrl(DEFAUL_URL + "get_sexuality");
        request.setListener(listener);
        HttpUtil.sendGetRequest(request);
    }


    public void clearAccount() {
        try {
            if (baseAccount != null) {
                setLocalLoginFlag(baseAccount.getUid(), false);
                PushManager.getInstance().deleteAlias();
                baseAccount = null;
                phoneAccount = null;
                weiboAccount = null;
                qqAccount = null;
                emailAccount = null;
                weixinAccount = null;
            }
            QLXNotificationCenter.getInstance().postNotify("kTFUserDidLogOutCompleteNotification", null);
            HttpRequestUtil.clearCookie();
            PersonalInfoService.getInstance().clearPersonalInfo();
            setLoginType(-1);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        try {
            switch (handleType) {
                case HANDLE_LOAD_ACCOUNT:
                    if (response.getStatus() == 0) {
                        JSONArray accounts = (JSONArray) response.getData();
                        initAccountInfo(accounts);
                        PersonalInfoService.getInstance().loadFans("", null);
                        PersonalInfoService.getInstance().loadAttentions("", null);

                        PersonalInfoService.getInstance().loadUnreadMessages();
                        PushManager.getInstance().addAlias();
                    }
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void initAccountInfo(JSONArray accounts) {
        try {
            for (int i = 0; i < accounts.length(); i++) {
                JSONObject accountData = accounts.getJSONObject(i);
                if (accountData.has(Constant.SOCIAL_TYPE)) {
                    int type = accountData.getInt(Constant.SOCIAL_TYPE);
                    if (type == 1) {
                        weiboAccount = new WeiboAccount();
                        weiboAccount.setJson(accountData);
                    } else if (type == 2) {
                        qqAccount = new QQAccount();
                        qqAccount.setJson(accountData);
                    } else if (type == 4) {
                        weixinAccount = new WeixinAccount();
                        weixinAccount.setJson(accountData);
                    }
                } else {
                    baseAccount = new PersonalBaseAccount();
                    baseAccount.setJson(accountData);
                    setLocalCounts(baseAccount);
                    setLocalCps(baseAccount);
                    MyAccount account = new MyAccount();
                    account.setAvatar(baseAccount.getUser_head_img());
                    account.setId(Integer.parseInt(baseAccount.getUid()));
                    account.setNickname(baseAccount.getNickname());
                    account.setVerified(true);
                    MyAccountOperation.setAccount(account);
                    if (accountData.has(Constant.MOBILE) && DiaobaoUtil.isMobileNO(accountData.getString(Constant.MOBILE))) {
                        phoneAccount = new PhoneAccount();
                        JSONObject phoneData = new JSONObject();
                        phoneData.put(Constant.MOBILE, accountData.getString(Constant.MOBILE));
                        phoneData.put(Constant.UID, baseAccount.getUid());
                        phoneAccount.setJson(phoneData);
                    }
                    TTLog.s("accountData==" + accountData.toString());
                    TTLog.s("accountData.has(Constant.EMAIL)" + accountData.has(Constant.EMAIL));
                    TTLog.s("accountData.has(Constant.EMAIL)===" + DiaobaoUtil.isEmail(accountData.getString(Constant.EMAIL)));
                    if (accountData.has(Constant.EMAIL) && DiaobaoUtil.isEmail(accountData.getString(Constant.EMAIL))) {
                        emailAccount = new EmailAccount();
                        JSONObject emailData = new JSONObject();
                        emailData.put(Constant.EMAIL, accountData.getString(Constant.EMAIL));
                        emailData.put(Constant.UID, baseAccount.getUid());
                        emailAccount.setJson(emailData);
                        TTLog.s("emailAccount====" + emailAccount.getEmail());
                    }
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public PersonalBaseAccount getBaseAccount() {
        return baseAccount;
    }

    public void setBaseAccount(PersonalBaseAccount account) {
        this.baseAccount = account;
    }

    public QQAccount getQqAccount() {
        return qqAccount;
    }

    public void setQqAccount(QQAccount qqAccount) {
        this.qqAccount = qqAccount;
    }

    public WeiboAccount getWeiboAccount() {
        return weiboAccount;
    }

    public void setWeiboAccount(WeiboAccount weiboAccount) {
        this.weiboAccount = weiboAccount;
    }

    public PhoneAccount getPhoneAccount() {
        return phoneAccount;
    }

    public void setPhoneAccount(PhoneAccount phoneAccount) {
        this.phoneAccount = phoneAccount;
    }

    public EmailAccount getEmailAccount() {
        return emailAccount;
    }

    public void setEmailAccount(EmailAccount emailAccount) {
        this.emailAccount = emailAccount;
    }

    public Bitmap getUserPortarit() {
        return userPortarit;
    }

    public void setUserPortarit(Bitmap userPortarit) {
        this.userPortarit = userPortarit;
    }

    public boolean isInfoChanged() {
        return infoChanged;
    }

    public void setInfoChanged(boolean infoChanged) {
        this.infoChanged = infoChanged;
    }

    public int getLoginType() {
        return loginType;
    }

    public void setLoginType(int loginType) {
        this.loginType = loginType;
        switch (loginType) {
            // 新浪微博登录
            case 1:
                WeiboService.getInstance().refreshTokenRequest(WeMediaApplication.applicationContext);
                break;
            //微信登录
//            case 4:
//                WeChatService.getInstance(WeMediaApplication.getInstance()).refreshTokenRequest();
//                break;

        }
    }

    public boolean isAccountBack() {
        return isAccountBack;
    }

    public void setAccountBack(boolean isAccountBack) {
        this.isAccountBack = isAccountBack;
    }

    public boolean isSocialLogin() {
        return socialLogin;
    }

    public void setSocialLogin(boolean socialLogin) {
        this.socialLogin = socialLogin;
    }

    public boolean isLogin() {
        if (baseAccount != null) {
            return true;
        } else {
        }
        return false;
    }

    public boolean isCacheLogin() {
        return cacheLogin;
    }

    public String getUserUid() {
        if (baseAccount != null && baseAccount.getUid() != null)
            return baseAccount.getUid();
        return "0";
    }


    public ArrayList<CpBean> getUserCps() {
        return userCps;
    }

    public void setUserCps(ArrayList<CpBean> cpsList) {
        userCps = cpsList;
    }

    public boolean checkAndLogin() {
        if (!isLogin()){
            jumpToLogin();
            return false;
        }
        return true;
    }


    public void jumpToLogin() {
        UVerifyManager.getInstance().verifyLogin(true);
//        SimpleWeexActivity.startActivity(WeMediaApplication.applicationContext, "vue/login/TFLoginRegister.js", Intent.FLAG_ACTIVITY_NEW_TASK);

    }


    public boolean isNotifyalbe() {

        String isNotify = context.getSharedPreferences(Constant.sharedPre_push, 0).getString("isnotifyalbe", "");
        if (isNotify.equalsIgnoreCase("t")) {
            return true;
        } else if (isNotify.equalsIgnoreCase("f")) {
            return false;
        }
        return true;
        // }
    }


    public boolean isUserSelf(String uid) {
        if (getUserUid() != null) {
            if (this.getUserUid().equals(uid)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 在用户新版本且第一次登录时，调用
     *
     * @param context
     */
    public static void newFunctionGuide(Context context) {
        SharedPreferences.Editor editor;
        SharedPreferences mySharedPreferences;
        int loginCount = 0;
        mySharedPreferences = context.getSharedPreferences("FirstLogin", Activity.MODE_PRIVATE);
        editor = mySharedPreferences.edit();
        loginCount = mySharedPreferences.getInt(Constant.LOGIN_COUNT + Constant.getVersionCode(context), 0);

        if (loginCount == 2 && AccountService.getInstance().isLogin()) {
            loginCount++;
            editor.putInt(Constant.LOGIN_COUNT + Constant.getVersionCode(context), loginCount);
            editor.commit();

        } else if (loginCount < 2 && AccountService.getInstance().isLogin()) {
            loginCount++;
            editor.putInt(Constant.LOGIN_COUNT + Constant.getVersionCode(context), loginCount);
            editor.commit();
        } else {
            ;
        }
    }

    public void setUserReadOrders(Set<String> userOrderList) {
        this.userReadOrders = userOrderList;
    }

    public Set<String> getUserReadOrders() {
        return userReadOrders;
    }

    public String getUserPortaritUrl() {
        return userPortaritUrl;
    }

    public void setUserPortaritUrl(String userPortaritUrl) {
        this.userPortaritUrl = userPortaritUrl;
    }


    public static void setAccountData(String json, JSCallback callback) {
        try {
            JSONObject data = new JSONObject(json);
            PersonalBaseAccount account = new PersonalBaseAccount();
            JSONObject userData = new JSONObject();
            String uid = DiaobaoUtil.getStringFromJSONObject(data, Constant.UID);
            userData.put(Constant.UID, uid);
            if (data.has("register_status")) {
                userData.put(Constant.USER_HEAD_IMG, DiaobaoUtil.getStringFromJSONObject(data, Constant.USER_HEAD_IMG));
            }
            userData.put(Constant.NICKNAME, DiaobaoUtil.getStringFromJSONObject(data, Constant.NICKNAME));
            int socilType = 0;
            if (data.has("login_type")) {
                socilType = data.getInt("login_type");
            }

            if (data.has("interest_type")) {
                int interest_type = data.getInt("interest_type");
                if (!TextUtils.isEmpty(uid) && !"0".equals(uid) && interest_type > 0) {
                    EventBusInterestTag event = new EventBusInterestTag();
                    event.setTag(EventBusInterestTag.TAG_INTEREST_TAG);
                    event.setType(interest_type);
                    EventBus.getDefault().post(event);
                    DiaoBaoSharedPreferences.setSharedPreferencesValueToBoolean("is_show_interest_tag_" + uid, false, WeMediaApplication.applicationContext);
                }
            }

            int loginType = 0;
            if (socilType == 1) {
                loginType = 1;
            }
            if (socilType == 3) {
                loginType = 2;
            }
            if (socilType == 2) {
                loginType = 4;
            }
            userData.put(Constant.LOGIN_TYPE, loginType);
            AccountService.getInstance().setLoginType(loginType);
            userData.put(Constant.MOBILE, DiaobaoUtil.getStringFromJSONObject(data, Constant.MOBILE));
            userData.put(Constant.EMAIL, DiaobaoUtil.getStringFromJSONObject(data, Constant.EMAIL));
            userData.put(Constant.SIGNATURE, DiaobaoUtil.getStringFromJSONObject(data, Constant.SIGNATURE));
            account.setJson(userData);
            if (!data.has("register_status") || !AccountService.getInstance().existUserCache(account.getUid())) {
                AccountService.getInstance().initLocalCachAccount(account);
            }
            AccountService.getInstance().setLocalLoginFlag(account.getUid(), true);
            AccountService.getInstance().setBaseAccount(account);
            AccountService.getInstance().loadLocalCounts();
            switch (socilType) {
                case 1:
                    AccountService.getInstance().bindAccount(data, AccountService.ACCOUNT_TYPE_WEIBO);
                    AccountService.getInstance().setLoginType(AccountService.ACCOUNT_TYPE_WEIBO);
                    break;
                case 2:
                    AccountService.getInstance().bindAccount(data, AccountService.ACCOUNT_TYPE_QQ);
                    AccountService.getInstance().setLoginType(AccountService.ACCOUNT_TYPE_QQ);
                    break;
                case 3:
                    AccountService.getInstance().bindAccount(data, AccountService.ACCOUNT_TYPE_WECHAT);
                    AccountService.getInstance().setLoginType(AccountService.ACCOUNT_TYPE_WECHAT);
                    break;
                default:
                    break;
            }
            PushManager.getInstance().addAlias();
            AccountService.getInstance().setSocialLogin(true);
            if (callback != null) {
                callback.invoke("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
